package com.hruiworks.campusgroup.algorithm.utils;

import com.hruiworks.campusgroup.functional.BooleanGetter;
import org.apache.commons.lang3.BooleanUtils;

import java.util.Collections;
import java.util.List;

/**
 * 计算多个区间是否有重叠
 */
public class OverLapCheckUtils {

    /**
     * 默认true 为开始，false为结束
     * @param list
     * @param positionGetter
     * @return 有重叠返回true，否则返回false
     * @param <T>
     */
    public static <T extends Comparable<T>> Boolean isOverLap(List<T> list, BooleanGetter<T> positionGetter) {
        Collections.sort(list);
        int activeIntervals = 0;
        for (T t : list) {
            Boolean b = positionGetter.get(t);
            if (BooleanUtils.isTrue(b)) {
                activeIntervals++;
                if (activeIntervals > 1) {
                    return true;
                }
            } else {
                activeIntervals--;
            }
        }

        return false;
    }
}
